package jmine.tec.test.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.io.IOUtils;

/**
 * Auxiliar para verificações feitas em diretório de alter.
 * 
 * @author lundberg
 */
public final class AlterDirectoryHelper {

    private static final byte[] UTF_8_BOM = new byte[]{ (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };

    /**
     * Construtor
     */
    private AlterDirectoryHelper() {
        // OK
    }

    /**
     * Verifica se o arquivo começa com o Byte Order Mark de arquivos UTF-8.
     * 
     * @param file file
     * @return boolean
     * @throws FileNotFoundException FileNotFoundException
     * @throws IOException IOException
     */
    public static boolean fileBeginsWithUTF8BOM(File file) throws FileNotFoundException, IOException {
        FileInputStream input = new FileInputStream(file);
        try {
            byte[] initialBytes = new byte[UTF_8_BOM.length];
            Arrays.fill(initialBytes, (byte) 0x00);
            input.read(initialBytes, 0, UTF_8_BOM.length);
            return Arrays.equals(UTF_8_BOM, initialBytes);
        } finally {
            IOUtils.closeQuietly(input);
        }
    }
}
